let behaviorsGetLocation = require('../../behaviors/getLocation')
const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
  showMsg
} from '../../../../utils/config.js'
import {
  reqConsumerHomeAdd
} from '../../../../request/api.js';

Page({
  behaviors: [behaviorsGetLocation],

  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离

    form: {
      homeName: '',
      lng: '',
      lat: '',
      address: '',
      rooms: [],
    },

    dataList: [{
      id: 1,
      name: '客厅',
    }, {
      id: 2,
      name: '主卧',
    }, {
      id: 3,
      name: '次卧',
    }, {
      id: 4,
      name: '餐厅',
    }, {
      id: 5,
      name: '厨房',
    }, {
      id: 6,
      name: '书房',
    }, ],
    roomResult: ['1', '2', '3', '4', '5', '6'],

    showInputDialog: false,

    confirmButtonDisabled: false,
  },

  onLoad(options) {

  },


  onShow() {

  },

  homeNameInput(e) {
    this.setData({
      'form.homeName': e.detail
    })
  },

  getLocation(location) {
    const {
      longitude,
      latitude,
      name
    } = location
    this.setData({
      'form.lng': longitude,
      'form.lat': latitude,
      'form.address': name,
    })
  },

  onChange(event) {
    this.setData({
      roomResult: event.detail,
    });
  },
  toggle(event) {
    const {
      index
    } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  goCreate() {
    wx.navigateTo({
      url: '/packageC/pages/room/add/add',
    })
  },


  onAddRoom() {
    this.setData({
      showInputDialog: true
    })
  },

  inputDialogHandle(e) {
    const {
      actionType,
      value
    } = e.detail
    this.setData({
      showInputDialog: false
    })
    if (actionType !== 'confirm') return
    let {
      dataList,
      roomResult
    } = this.data
    dataList.push({
      id: `${dataList.length + 1}`,
      name: value,
    })
    console.log(dataList.length);
    roomResult.push(`${dataList.length}`)
    this.setData({
      dataList,
      roomResult,
    }, () => {
      const checkbox = this.selectComponent(`.checkboxes-${dataList.length}`);
      checkbox.toggle();
    });
  },

  async confirm() {
    if (this.data.confirmButtonDisabled) return
    const {
      homeName,
      lng,
      lat,
      address,
    } = this.data.form
    if (!homeName) return showMsg("请输入家庭名称")
    if (!lng) return showMsg("请选择城市位置")

    // 过滤出 roomResult 中存在的 id 对应的项
    const {
      dataList,
      roomResult
    } = this.data
    const rooms = dataList
      .filter(item => roomResult.includes(item.id.toString()))
      .map(item => item.name);

    const res = await reqConsumerHomeAdd({
      userId: app.globalData.userId,
      homeName,
      lng,
      lat,
      address,
      rooms
    })
    if (res.code != 20000) return showMsg(res.message);
    showMsg("创建成功", 'success')
    this.setData({
      confirmButtonDisabled: true
    })
    // app.globalData.isUpdateCIndex = true
    // wx.setStorageSync('isUpdateCIndex', true);
    setTimeout(() => {
      this.setData({
        confirmButtonDisabled: false
      })
      this.notifyPageUpdataFoo()
      wx.navigateBack()
    }, 1000)
  },


  notifyPageUpdataFoo() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('receiveDataFromDetail', {
      isUpdataList: true
    });
  }




})